<template>

<div id="updateShareCode">
  <el-row>
    <mu-appbar title="修改分成比例" style="background-color:#409EFF">
      <mu-icon-button  icon="arrow_back_ios" slot="left" :to="{name: 'GmMenu' }"/>
    </mu-appbar>
  </el-row>

  <el-row>
    <el-col :span="12">
    </el-col>
    <el-col :span="12">
      <i class="el-icon-arrow-down" style="float:right" @click="handleTools"></i>
    </el-col>
  </el-row>

  <div class= "tools" v-show="tools">
    <el-row>
      <el-col :xs="6" :sm="4" :md="3" :lg="3">
        运营商ID
      </el-col>
      <el-col :xs="18" :sm="20" :md="21" :lg="21">
        <el-input v-model="condition.uid" size="mini" @blur="getCa"></el-input>
      </el-col>
    </el-row>
      <!-- <el-col :xs="24" :sm="8" :md="8" :lg="8">
        <el-col :xs="6" :sm="6" :md="6" :lg="6">
          起始日期
        </el-col>
        <el-col :xs="18" :sm="18" :md="18" :lg="18">
          <el-date-picker
            v-model="condition.startDay"
            type="date"
            placeholder="选择起始日期"
            size="mini"
            @change="getCa"
            value-format="yyyy-MM-dd">
          </el-date-picker>
        </el-col>
      </el-col>

      <el-col :xs="24" :sm="8" :md="8" :lg="8">
        <el-col :xs="6" :sm="6" :md="6" :lg="6">
          结束日期
        </el-col>
        <el-col :xs="18" :sm="18" :md="18" :lg="18">
          <el-date-picker
            v-model="condition.endDay"
            type="date"
            placeholder="选择结束日期"
            size="mini"
            @change="getCa"
            value-format="yyyy-MM-dd">
          </el-date-picker>
        </el-col>
      </el-col> -->

  </div>

  <el-dialog title="修改领取比例" :visible.sync="dialogFormVisible">
    <el-form :model="form">
      <el-form-item label="运营商ID">
        <el-input v-model="form.uid" readonly=true size="mini"></el-input>
      </el-form-item>
      <el-form-item label="新领取比例">
        <el-input v-model="form.division" size="mini"></el-input>
      </el-form-item>
      <el-form-item label="新营收等级">
        <el-input v-model="form.orgDisLevel" size="mini"></el-input>
      </el-form-item>
    </el-form>
    <div slot="footer" class="dialog-footer">
      <el-button type="primary" @click="dialogFormVisible = false">取 消</el-button>
      <el-button type="primary" @click="handleSubmit">确 定</el-button>
    </div>
  </el-dialog>

  <el-dialog title="修改运营比例" :visible.sync="dialogFormVisible1">
    <span style="color:red;" v-if="this.isDefault">当前比例为默认比例，点击创建后生产私有比例然后进行修改</span>
    <el-table
      :data="setup"
      style="width: 100%">
      <el-table-column
        prop="agentLevel"
        label="代理级别">
        <template slot-scope="scope">
          <span v-if="scope.row.agentLevel === 1">二级代理</span>
          <span v-if="scope.row.agentLevel === 2">一级代理</span>
          <span v-if="scope.row.agentLevel === 3">总代理</span>
          <span v-if="scope.row.agentLevel === 4">运营商</span>
          <span v-if="scope.row.agentLevel === 6">白户</span>
          <span v-if="scope.row.agentLevel === 5">官方</span>
        </template>
      </el-table-column>

      <el-table-column
        prop="ratio"
        label="运营比例">
        <template slot-scope="scope">
          <el-input v-model="scope.row.ratio" size="mini"></el-input>
        </template>
      </el-table-column>

    </el-table>
    <div slot="footer" class="dialog-footer">
      <el-button type="primary" @click="dialogFormVisible1 = false">取 消</el-button>
      <el-button v-if="this.isPrivate" type="primary" @click="updatePrivateSetUp">修改</el-button>
      <el-button v-if="this.isDefault" type="primary" @click="initSetUp">创 建</el-button>
    </div>
  </el-dialog>

  <el-row>
    <el-table
      :data="tableData"
      style="width: 100%"
      size="mini">
      <el-table-column
        prop="carrieroperatorUid"
        label="运营商ID">
      </el-table-column>
      <el-table-column
        prop="realName"
        label="姓名">
      </el-table-column>
      <el-table-column
        prop="division"
        label="领取比例">
      </el-table-column>
      <el-table-column
        prop="orgDisLevel"
        label="营收等级">
      </el-table-column>
      <el-table-column
        prop="headurl"
        label="密码">
      </el-table-column>
      <el-table-column
        prop="createTime"
        label="创建时间">
      </el-table-column>
      <el-table-column label="操作">
        <template slot-scope="scope">
          <el-button
            size="mini"
            @click="handleUpdate(scope.row)">修改领取比例</el-button>
        </template>
      </el-table-column>
      <el-table-column label="操作">
        <template slot-scope="scope">
          <el-button
            size="mini"
            @click="handleUpdateDefault(scope.row.carrieroperatorUid)">修改营收比例</el-button>
        </template>
      </el-table-column>
    </el-table>
  </el-row>

  <el-row>
    <center>
      <el-pagination
        small
        layout="prev, pager, next"
        :total="condition.total"
        :page-size="condition.pageSize"
        :current-page="condition.currentPage"
        @current-change="handleCurrentChange">
      </el-pagination>
    </center>
  </el-row>
</div>

</template>

<style>
#updateShareCode .el-dialog {
  width: 70%
}
#updateShareCode .el-row {
    margin-bottom: 16px;
  }
#updateShareCode .el-form-item__content {
    margin-bottom: -24px
  }
#updateShareCode form.el-form {
    margin-top: -24px;
  }
</style>

<script>
  export default {
    data () {
      return {
        form: {
          uid: '',
          division: '',
          orgDisLevel: '',
          account: sessionStorage.getItem('account')
        },
        condition: {
          uid: '',
          currentPage: 1,
          pageSize: 10,
          total: 0,
          account: sessionStorage.getItem('account')
        },
        setup: [],
        dialogFormVisible: false,
        dialogFormVisible1: false,
        tools: true,
        tableData: [],
        isPrivate: false,
        isDefault: false,
        uid: '' // 记录初始化哪个caId
      }
    },
    methods: {
      validate () {
        for (let item in this.form) {
          if (this.form[item].length === 0) {
            this.$message.error('请完善信息')
            return false
          }
        }
        return true
      },
      handleTools () {
        if (this.tools) {
          this.tools = false
        } else {
          this.tools = true
        }
      },
      init () {
        this.getCa()
      },
      getCa () {
        let that = this
        if (this.condition.uid === '' || this.condition.uid.length === 0) {
          this.condition.uid = -1
        }
        this.$axios({
          method: 'get',
          url: '/api/CarrieroperatorService.getCarrieroperatorsForUpdateDivision',
          params: this.condition
        })
        .then(function (resp) {
          that.condition.uid = ''
          if (resp.data.status === 'success') {
            that.tableData = resp.data.data
            that.condition.total = resp.data.page.total
          }
        })
      },
      handleUpdate (row) {
        this.dialogFormVisible = true
        this.form.uid = row.carrieroperatorUid
        this.form.division = row.division
        this.form.orgDisLevel = row.orgDisLevel
      },
      handleUpdateDefault (caId) {
        this.dialogFormVisible1 = true
        this.uid = caId
        let that = this
        this.$axios({
          method: 'get',
          url: '/api/CarrieroperatorService.getDefaultAgentSetUp',
          params: {'uid': caId}
        })
        .then(function (resp) {
          that.condition.uid = ''
          if (resp.data.status === 'success') {
            that.setup = resp.data.data
            if (resp.data.info === 'default') {
              that.isDefault = true
              that.isPrivate = false
            } else {
              that.isDefault = false
              that.isPrivate = true
            }
          }
        })
        this.dialogFormVisible1 = true
      },
      handleSubmit () {
        if (typeof Number(this.form.division) !== 'number' || this.form.division <= 0 || this.form.division >= 1) {
          this.$message({
            showClose: true,
            message: '领取比例错误',
            type: 'success'
          })
          return false
        }
        if (typeof Number(this.form.orgDisLevel) !== 'number' || Number(this.form.orgDisLevel) % 1 !== 0 || Number(this.form.orgDisLevel) <= 0 || Number(this.form.orgDisLevel) > 100) {
          this.$message({
            showClose: true,
            message: '营收等级错误',
            type: 'success'
          })
          return false
        }

        let that = this
        if (this.validate()) {
          this.$axios({
            method: 'get',
            url: '/api/CarrieroperatorService.updateDivision',
            params: this.form
          })
          .then(function (resp) {
            if (resp.data.status === 'success') {
              that.$message({
                showClose: true,
                message: '修改成功',
                type: 'success'
              })
              that.getCa()
              that.dialogFormVisible = false
            } else {
              that.$message({
                showClose: true,
                message: resp.data.cause,
                type: 'warning'
              })
            }
          })
        }
      },
      handleCurrentChange (val) {
        this.condition.currentPage = val
        this.getCa()
      },
      initSetUp () {
        let that = this
        this.$axios({
          method: 'get',
          url: '/api/CarrieroperatorService.initSetUp',
          params: {'uid': that.uid}
        })
        .then(function (resp) {
          if (resp.data.status === 'success') {
            that.handleUpdateDefault(that.uid)
          } else {
            that.$message({
              showClose: true,
              message: resp.data.cause,
              type: 'warning'
            })
          }
        })
      },
      updatePrivateSetUp () {
        let that = this
        for (let i = 0; i < this.setup.length; i++) {
          let ratio = this.setup[i].ratio
          if (ratio === '' || ratio === null) {
            this.$message({
              showClose: true,
              message: '比例错误',
              type: 'warning'
            })
            return false
          }
          if (isNaN(ratio)) {
            this.$message({
              showClose: true,
              message: '比例错误',
              type: 'warning'
            })
            return false
          }
          if (ratio < 0 || ratio > 0.8) {
            if (isNaN(ratio)) {
              this.$message({
                showClose: true,
                message: '比例错误',
                type: 'warning'
              })
              return false
            }
          }
        }
        this.$axios({
          method: 'get',
          url: '/api/CarrieroperatorService.updatePrivateSetUp',
          params: {'ratios': JSON.stringify(this.setup)}
        })
        .then(function (resp) {
          if (resp.data.status === 'success') {
            that.dialogFormVisible1 = false
            that.$message({
              showClose: true,
              message: '修改成功',
              type: 'success'
            })
          } else {
            that.$message({
              showClose: true,
              message: resp.data.cause,
              type: 'warning'
            })
          }
        })
      }
    },
    mounted () {
      this.init()
    }
  }
</script>
